package com.ggy.babybed.socketwork.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import com.ggy.babybed.socketwork.NetworkConstant;

/**
 * 网络工具类，功能包括：1.判断网络连接状况， 2.判断网络类型
 *
 * @author gonggy
 */
public final class NetworkUtil {

    /**
     * 判断网络连接状况
     *
     * @param context 上下文
     * @return true：网络可用， false：网络不可用
     */
    public static boolean isConnected(Context context) {
        if (null != context) {
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if(null != manager){
                NetworkInfo info = manager.getActiveNetworkInfo();
                if (null != info) {
                    return info.isAvailable() && info.isConnected();
                }
            }

        }
        return false;
    }

    /**
     * 获取网络类型
     *
     * @param context
     * @return 网络类型：2G,3G,4G,WIFI
     */
    public static String getNetworkType(Context context) {
        if (null != context) {
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = manager.getActiveNetworkInfo();
            if (null != info && info.isConnected()) {
                if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                    return NetworkConstant.WIFI;
                } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                    switch (info.getSubtype()) {
                        case TelephonyManager.NETWORK_TYPE_GPRS:
                        case TelephonyManager.NETWORK_TYPE_CDMA:
                        case TelephonyManager.NETWORK_TYPE_EDGE:
                        case TelephonyManager.NETWORK_TYPE_IDEN:
                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                            return NetworkConstant._2G;
                        case TelephonyManager.NETWORK_TYPE_UMTS:
                        case TelephonyManager.NETWORK_TYPE_HSPA:
                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                        case TelephonyManager.NETWORK_TYPE_EHRPD:  //api<11 : replace by 12
                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                        case TelephonyManager.NETWORK_TYPE_EVDO_A:
                        case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
                            return NetworkConstant._3G;
                        case TelephonyManager.NETWORK_TYPE_LTE:    //api<11 : replace by 13
                            return NetworkConstant._4G;
                        default:
                            if (info.getSubtypeName().equalsIgnoreCase("TD-SCDMA")
                                    || info.getSubtypeName().equalsIgnoreCase("WCDMA")
                                    || info.getSubtypeName().equalsIgnoreCase("CDMA2000")) {
                                return NetworkConstant._3G;
                            } else {
                                return info.getSubtypeName();
                            }
                    }
                }
            }
        }
        return NetworkConstant.NONE;
    }
}

